package com.yifeng.repo.base.utils.converter;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @author hx
 * @date 2019/5/5
 */
public class AmountHelper {

    public static BigDecimal c2y(Integer amount) {
        return new BigDecimal(amount).divide(new BigDecimal(100), 2, RoundingMode.HALF_DOWN);
    }

    public static BigDecimal y2c(String amount) {
        return new BigDecimal(amount).multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_DOWN);
    }

    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) {
        return dividend.divide(divisor, 10, RoundingMode.DOWN);
    }

    public static BigDecimal multiply(BigDecimal multiplier, BigDecimal multiplicand) {
        return multiplier.multiply(multiplicand, new MathContext(10, RoundingMode.DOWN));
    }

    public static BigDecimal multiply(BigDecimal multiplier, Integer percent) {
        return multiplier.multiply(new BigDecimal(percent), new MathContext(10, RoundingMode.DOWN))
                .divide(new BigDecimal(100), 2, RoundingMode.HALF_DOWN);
    }

    public static Integer totalAmount(BigDecimal quantity, Integer exercisePrice) {
        return quantity.multiply(new BigDecimal(exercisePrice)).setScale(0, RoundingMode.HALF_DOWN).intValue();
    }

    /**
     * double的四舍五入
     */
    public static double doubleRound(double value) {
        return new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static double doubleRound(double value, int scale) {
        return new BigDecimal(value).setScale(scale, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     * 取正
     */
    public static BigDecimal abs(BigDecimal value) {
        return value.compareTo(BigDecimal.ZERO) < 0 ? value.negate() : value;
    }

}
